import { useCallback, useEffect, useState } from 'react';
import useBasisCash from './useBasisCash';
import { TokenStat } from '../basis-cash/types';
import config from '../config';
import { openGroup } from '../views/Banks/Banks';

const useLiquidityOracle = () => {
  const [stat, setStat] = useState('');
  const basisCash = useBasisCash();

  const fetchLiquidityOracle = useCallback(async () => {
    if (config.plane[config.group].sCASH != null) {
      const stat = await basisCash.getCashStatFromUniswap();
      setStat(stat.totalSupply);
    } else if (config.plane[config.group].lp != null) {
      const oracle = await basisCash.getLiquidityOracle(config.group);
      setStat(oracle.tvl);
    }
    
  }, [basisCash, config.group]);

  useEffect(() => {
    fetchLiquidityOracle().catch((err) => console.error(`Failed to fetch SNB price: useCashStats ${err.stack}`));
    const refreshInterval = setInterval(fetchLiquidityOracle, config.refreshInterval);
    return () => clearInterval(refreshInterval);
  }, [basisCash, config.group]);

  return stat;
};

export default useLiquidityOracle;
